<?php
function adm_get_lang_default(){
    global $anyDB, $tb_fix,$_SESSION;
    if(empty($_SESSION['mctadmlang'])){
        $_SESSION['mctadmlang'] = 'vi';
        $lag_return = "Tiếng Việt";
    }else{
        $langcode = $_SESSION['mctadmlang'];
        $sql = "select lang_name from ".$tb_fix."_lang where lang_code = '$langcode'";
        $r = $anyDB->getNext($anyDB->query($sql));
        $lag_return = $r['lang_name'];
    }
    return $lag_return;
}
function _html_Error()
{
    global $temp;
	$_html = $temp->fetch( '_adm_error.mct' );
	return $_html;
}

function _html_ChangePass()
{
	global $temp;
   
	$_html = $temp->fetch( '_adm_change_pass.mct' );
	return $_html;
}

function _html_Home()
{
    global $admin_id,$admin_user,$temp;
    $temp->assign("adm_user",$admin_user);
    $temp->assign( 'last_visited', "".visited($admin_id)."" );
	$_html = $temp->fetch('_adm_home.mct');
	return $_html;
}

function _html_trans($_msg="Có lỗi xẩy ra, bạn đang được chuyển về trang chủ.", $_url=current_link)
{
	global $dir_tpl,$temp;
	$temp->assign( 'base_tpl', "".$dir_tpl."" );
	$temp->assign( '_msg', "".$_msg."" );
	$temp->assign( '_url', "".$_url."" );
	$_html = $temp->fetch( '_adm_trans.mct' );
	return $_html;
}

// breakcum

function breakcum($here=""){
    global $dir_tpl;
    $html = "";  
    $html .= "
        <div class=\"page-title ui-widget-content ui-corner-all\">
					<h1>Trang chủ: <b>$here</b></h1>
				</div>
    ";
    return $html;
}

function visited($uid,$action = ""){
    global $tb_fix,$mysql;
    $time = time();
    $r = $mysql->fetch_array($mysql->query("select last_visited,login_time from ".$tb_fix."_admin where a_id = $uid"));
    $last_vs = $r['last_visited'];
    $login_time = $r['login_time'];    
    if($action == "update"){
       $mysql->query("update ".$tb_fix."_admin set last_visited = '$login_time' where a_id = $uid");
       return; 
    }else{
        if($last_vs == 0){
           $mysql->query("update ".$tb_fix."_admin set last_visited = '$time' where a_id = $uid");
           $r = $mysql->fetch_array($mysql->query("select last_visited from ".$tb_fix."_admin where a_id = $uid"));
           $last = $r['last_visited'];
        }else{
           $r = $mysql->fetch_array($mysql->query("select last_visited from ".$tb_fix."_admin where a_id = $uid"));
           $last = $r['last_visited'];
        }
        return gmdates($last);    
    }
}
// login
function check_login(){
    global $tb_fix,$mysql,$dir_tpl,$admin_session,$shop_name,$temp;
    if(empty($_SESSION["admin_6687"])){
    	$login= isset($_POST["login"]) ? stripslashes(urldecode($_POST["login"])) : "";
    	$error ="";
    	if ($login) {
    		$username= isset($_POST["username"]) ? stripslashes(txt_HTML($_POST["username"])) : "";
    		$password= isset($_POST["password"]) ? md5(stripslashes(txt_HTML($_POST["password"]))) : "";
            
    		$q = $mysql->query("SELECT * FROM ".$tb_fix."_admin WHERE username = '".$username."' AND pwd = '".$password."' LIMIT 1");
    		$t = $mysql->num_rows($q);
    		if ($t>0) {
    			$r = $mysql->fetch_array($q);
                //update login time
                $mysql->query("update ".$tb_fix."_admin set login_time = ".time()." where a_id = ".$r['a_id']."");
                $admin_session = base64_encode("$r[a_id]:$username:$password:$r[fullname]:$r[email]");
    			$_SESSION["admin_6687"] = "$admin_session";
                @Header("Location: admin.php");
    		}
    		else $error ="error";
    	}
    	$temp->assign( 'error', "".$error."" );
    	$temp->assign( 'base_tpl', "".$dir_tpl."" );
    	$temp->assign( 'ip_client', "".ip_client."" );
        $temp->assign( '_cf_shop_name' ,$shop_name);
    	$temp->display( '_admin_login.mct' );
    	exit();
    }else{
        $admin_session = $_SESSION["admin_6687"];  
        $_SESSION["login"] = "$admin_session";  
    }
}
//ghi nho cong viec
function ghi_nho_cv(){
    global $tb_fix,$mysql,$dir_tpl,$admin_session;
    if(isset($admin_session)){
        list($adm_id,$adm_pass,$adm_name,$adm_email)= explode(":",base64_decode($admin_session));
        $sql = "select * from ".$tb_fix."_ghi_nho where gnh_user_id = $adm_id";
        $q = $mysql->query($sql);
        $t = $mysql->num_rows($q);
        if($t>0){
            $rows = $mysql->fetch_array($q);
            $rows['gnh_noi_dung'] = nl2br($rows['gnh_noi_dung']); 
            $rows['gnh_thoi_gian'] = gmdates($rows['gnh_thoi_gian']);
            $rows['gnh_status']=1;
        }else{
            $rows['gnh_user_id']=$adm_id;
            $rows['gnh_status']=2;
        }
    }
return $rows;
}
// valid url
function isURL($url = NULL) {
        if($url==NULL) return false;

        $protocol = '(http://|https://|ftp://)';
        $allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';

        $regex = "^". $protocol . // must include the protocol
                         '(' . $allowed . '{1,63}\.)+'. // 1 or several sub domains with a max of 63 chars
                         '[a-z]' . '{2,6}'; // followed by a TLD
        if(eregi($regex, $url)==true) return true;
        else return false;
}
?>